package com.base.leetcode;

/**
 * @Author: hdhao
 * @Create: 2023/4/5 - 14:45
 * @Version: v1.0
 */
//求数据流中第K大的元素
public class Leetcode703 {
    private MinHeap heap;
    public Leetcode703(int k, int[] nums) {
        heap = new MinHeap(k);
        for (int num : nums) {
            add(num);
        }
    }

    //此方法会被不断调用,模拟数据流中新来的元素
    public int add(int val) {
        if (!heap.isFull()) {
            heap.offer(val);
        }else if (val > heap.peek()) {
            heap.replace(val);
        }
        return heap.peek();
    }
}
